package com.taxx.schedule;

import com.taxx.controller.dto.TaxIdentityReportReqDTO;
import com.taxx.controller.dto.TaxIncomeReportReqDTO;
import com.taxx.controller.dto.TaxPlatformReportReqDTO;
import com.taxx.service.TaxReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 税务报送定时任务
 *
 * @author taxx
 * @since 2024-01-01
 */
@Slf4j
//@Component
public class TaxReportSchedule {

    @Autowired
    private TaxReportService taxReportService;

    /**
     * 每天凌晨2点执行收入信息报送
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void reportIncomeData() {
        log.info("定时任务开始执行收入信息报送");
        try {
            Map<String, Object> result = taxReportService.reportIncomeData(new TaxIncomeReportReqDTO());
            log.info("定时任务收入信息报送完成，结果：{}", result);
        } catch (Exception e) {
            log.error("定时任务收入信息报送失败", e);
        }
    }

    /**
     * 每天凌晨3点执行身份信息报送
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void reportIdentityData() {
        log.info("定时任务开始执行身份信息报送");
        try {
            Map<String, Object> result = taxReportService.reportIdentityData(new TaxIdentityReportReqDTO());
            log.info("定时任务身份信息报送完成，结果：{}", result);
        } catch (Exception e) {
            log.error("定时任务身份信息报送失败", e);
        }
    }

    /**
     * 每天凌晨4点执行平台信息报送
     */
    @Scheduled(cron = "0 0 4 * * ?")
    public void reportPlatformData() {
        log.info("定时任务开始执行平台信息报送");
        try {
            Map<String, Object> result = taxReportService.reportPlatformData(new TaxPlatformReportReqDTO());
            log.info("定时任务平台信息报送完成，结果：{}", result);
        } catch (Exception e) {
            log.error("定时任务平台信息报送失败", e);
        }
    }

    /**
     * 每小时执行一次失败重试
     */
    @Scheduled(cron = "0 0 * * * ?")
    public void retryFailedReports() {
        log.info("定时任务开始执行失败重试");
        try {
            // 重试收入信息
            Map<String, Object> incomeResult = taxReportService.retryFailedReport("income");
            log.info("定时任务收入信息重试完成，结果：{}", incomeResult);

            // 重试身份信息
            Map<String, Object> identityResult = taxReportService.retryFailedReport("identity");
            log.info("定时任务身份信息重试完成，结果：{}", identityResult);

            // 重试平台信息
            Map<String, Object> platformResult = taxReportService.retryFailedReport("platform");
            log.info("定时任务平台信息重试完成，结果：{}", platformResult);

        } catch (Exception e) {
            log.error("定时任务失败重试失败", e);
        }
    }
}
